package src.main.zuhe;

import src.main.danli.IdlerSingleInnerClass;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        // 中国
        CityContainer china = new CityContainer("china");

        // 上海
        Container shangHai = new City(10,"shangHai");

        // 山东
        CityContainer shanDong = new CityContainer("shanDong");
        // 青岛
        Container qingDao = new City(20,"qingDao");
        // 烟台
        Container yanTai = new City(30,"yanTai");

        //四川
        CityContainer sichuan=new CityContainer("sichuan");
        //成都
        Container chengdu=new City(100,"chengdu");
        Container mianyang=new City(99,"mianyang");

        //重庆
        CityContainer chongqing=new CityContainer("chongqing");
        CityContainer wanzhou=new CityContainer("wanzhou");
        City xian1=new City(21,"xian1");
        City xian2=new City(22,"xian2");
        // 四级模式
        wanzhou.addCity(xian1);
        wanzhou.addCity(xian2);
        chongqing.addCity(wanzhou);


        // 为山东添加城市
        shanDong.addCity(qingDao);
        shanDong.addCity(yanTai);

        //四川城市
        sichuan.addCity(chengdu);
        sichuan.addCity(mianyang);

        // 为中国添加城市
        china.addCity(shangHai);
        china.addCity(shanDong);
        china.addCity(sichuan);
        china.addCity(chongqing);
        System.out.println("中国人口" + china.count());

    }
}
    /**
     * 统计接口
     */
interface Container {
         String getName() ;
        // 统计方法
         int count();

    }


    /**
     * 城市 节点
     */
class City implements Container{
        // 城市人口数量
        private Integer sum;

        public String getName() {
            return name;
        }

        private String  name;
        // 构造方法
        public City(Integer sum,String name) {
            this.sum = sum;
            this.name=name;
        }

        @Override
        public int count() {
            System.out.println("城市统计完毕返回人数！");
            return sum;
        }
    }


    /**
     * 城市容器
     */
class CityContainer implements  Container {
        private String name;
        public String getName() {
            return name;
        }
        public CityContainer(String name){
            this.name=name;
        }


        // 城市集合
        private List<Container> containerList = new ArrayList<>();

        // 添加城市
        public void addCity(Container container) {
            containerList.add(container);
        }

        // 删除城市
        public void deleteCity(Container container) {
            containerList.remove(container);
        }

        // 查询城市集和
        public List<Container> getContainerList() {
            return containerList;
        }
        /**此处输出是关键，在外被调用类似递归*/
        @Override
        public int count() {
            int sumPerson = 0;
            int i=1;
            for (Container c: containerList) {
                System.out.println("运行次数:"+c.getName()+":::"+i++);
                sumPerson += c.count();
            }

            return sumPerson;
        }
    }